<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%-- 页头 --%>
    <title>添加文章分类 - ReadJava教程</title>
    <%@include file="../common/head.jsp" %>
</head>
<body>
<div class="wrapper">

    <%-- 导航 --%>
    <%@include file="manage-header.jsp" %>

    <%-- 侧边栏 --%>
    <%@include file="manage-sidebar.jsp" %>

    <div class="main-panel">
        <div class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <div class="card-title">新增分类</div>
                            </div>
                            <div class="card-body">
                                <div class="row justify-content-md-center">
                                    <div class="col-md-8">
                                        <div class="card">
                                            <div class="card-body">
                                                <form id="addAcForm">
                                                    <div class="form-group">
                                                        <label>分类标题</label>
                                                        <input type="text" name="acTitle" class="form-control input-square acTitle">
                                                    </div>
                                                    <div class="form-group">
                                                        <label>分类介绍</label>
                                                        <input type="text" name="acIntroduce" class="form-control input-square acIntroduce">
                                                    </div>
                                                    <div class="form-group">
                                                        <label>分类图标</label>
                                                        <input type="text" name="acIcon" class="form-control input-square acIcon">
                                                    </div>
                                                    <div class="form-group text-center">
                                                        <label class="form-radio-label">
                                                            <input class="form-radio-input acState" type="radio"
                                                                   name="acState" value="0" checked>
                                                            <span class="form-radio-sign">启用分类</span>
                                                        </label>
                                                        <label class="form-radio-label">
                                                            <input class="form-radio-input acState" type="radio"
                                                                   name="acState" value="1">
                                                            <span class="form-radio-sign">禁用分类</span>
                                                        </label>
                                                    </div>
                                                </form>
                                            </div>
                                            <div class="card-action text-center">
                                                <button type="button" class="btn btn-info btn-success" id="addAcBtn">
                                                    新增分类
                                                </button>
                                                <button type="button" class="btn btn-danger"
                                                        onclick="window.location.href='/manage/article/category/list'">
                                                    返回管理
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

<%-- js --%>
<%@include file="../common/script.jsp" %>

<script>
    $(function () {
        // 新增分类
        $("#addAcBtn").on("click", function () {
            var acTitle = $(".acTitle").val();
            var acIntroduce = $(".acIntroduce").val();
            var acIcon = $(".acIcon").val();
            var acState = $('.acState:checked').val();
            if (acTitle != null && acTitle.length != 0) {
                if (acIntroduce != null && acIntroduce.length != 0) {
                    if (acIcon != null && acIcon.length != 0) {
                        if (acState != null && acState.length != 0) {
                            $.ajax({
                                type: "POST",
                                url: "/manage/article/category/add",
                                data: {
                                    acTitle: acTitle,
                                    acIntroduce: acIntroduce,
                                    acIcon: acIcon,
                                    acState: acState
                                },
                                dataType: "json",
                                success: function (data) {
                                    if (data.status == 200) {
                                        alert(data.message);
                                        window.location.reload();
                                    }
                                },
                                error: function () {
                                    alert("操作失败：Ajax更新错误");
                                }
                            });
                        } else {
                            alert("操作失败：未设置分类状态");
                        }
                    } else {
                        alert("操作失败：分类图标不能为空");
                    }
                } else {
                    alert("操作失败：分类介绍不能为空");
                }
            } else {
                alert("操作失败：分类标题不能为空");
            }
        });
    });
</script>
</html>